home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 1996 September / Software of the Month Club 1996 September.iso / mac / Software Research Institute-SRI / Business / Alpha ƒ / Tcl / SystemCode / c.tcl < prev    next >
Encoding:
Text File  |  1995-12-14  |  11.1 KB  |  402 lines  |  [TEXT/ALFA]

  1.  
  2. newModeVar C elecColon {1} 1
  3. newModeVar C elecRBrace {1} 1
  4. newModeVar C leftFillColumn {3} 0
  5. newModeVar C prefixString {//} 0 
  6. newModeVar C electricSemi {1} 1
  7. newModeVar C wordBreak {[a-zA-Z0-9_]+} 0
  8. newModeVar C elecLBrace {1} 1
  9. newModeVar C elecElse {1} 1
  10. newModeVar C wordWrap {0} 1
  11. newModeVar C funcExpr {^[^ \t\(#\r/@].*\(.*\)$} 0
  12. newModeVar C wordBreakPreface {[^a-zA-Z0-9_]} 0
  13. newModeVar C electricTab {0} 1
  14. newModeVar C autoMark    0    1
  15.  
  16. set cCommentRegexp    {/\*(([^*]/)|[^*]|\r)*\*/}
  17. set cPreRegexp        {^\#[\t ]*[a-z]*}
  18. set    cKeyWords    {
  19.     void break register short enum extern int for if while struct static long continue
  20.     switch case char unsigned double float return else default goto do pascal Boolean
  21.     typedef volatile union auto sizeof size_t
  22. }
  23. if {[info exists Cwords]} {set cKeyWords [concat $cKeyWords $Cwords]}
  24. regModeKeywords -e {//} -b {/*} {*/} -c red -m {#} -k blue C $cKeyWords
  25. # regModeKeywords -a -u -k blue C {pete keleher}
  26. # regModeKeywords -e {//} -b {/*} {*/} -c red -m {#} -k blue C $cKeyWords -i "\}" -i "\{" -I green
  27.  
  28. #================================================================================
  29.  
  30. newModeVar C++ elecColon {1} 1
  31. newModeVar C++ elecRBrace {1} 1
  32. newModeVar C++ leftFillColumn {3} 0
  33. newModeVar C++ prefixString {//} 0
  34. newModeVar C++ electricSemi {1} 1
  35. newModeVar C++ wordBreak {[a-zA-Z0-9_]+} 0
  36. newModeVar C++ elecLBrace {1} 1
  37. newModeVar C++ elecElse {1} 1
  38. newModeVar C++ wordWrap {0} 1
  39. newModeVar C++ funcExpr {^[^ \t\(#\r/@].*\(.*\)$} 0
  40. newModeVar C++ wordBreakPreface {[^a-zA-Z0-9_]} 0
  41. newModeVar C++ electricTab {1} 1
  42. newModeVar C++ autoMark        0    1
  43.  
  44.  
  45. set {c++KeyWords} {
  46.     new delete class friend protected private public template 
  47.     try catch throw operator const mutable virtual asm inline this
  48.     and and_eq bitand bitor compl not or or_eq xor xor_eq not_eq
  49.     wchar_t bool true false
  50.     static_cast dynamic_cast reinterpret_cast typeid
  51.     using namespace inherited
  52. }
  53. if {[info exists {C++words}]} {
  54.     set {c++KeyWords} [concat ${c++KeyWords} ${C++words} $cKeyWords]
  55. } else {
  56.     set {c++KeyWords} [concat ${c++KeyWords} $cKeyWords]
  57. }
  58.  
  59. regModeKeywords -e {//} -b {/*} {*/} -c red -m {#} -k blue {C++} ${c++KeyWords}
  60. # regModeKeywords -e {//} -b {/*} {*/} -c red -m {#} -k blue {C++} ${c++KeyWords} -i "\{" -i "\}" -I green
  61. unset cKeyWords
  62. unset {c++KeyWords}
  63.  
  64. #=============================================================================
  65. # "Electric" C functions.
  66. #=============================================================================
  67.  
  68. # returns the indent string of the line named by 'pos'
  69. proc indentString pos {
  70.     set start [lineStart $pos]
  71.     set end [nextLineStart $pos]
  72.     set text [getText $start $end]
  73.     for {set i 0} {1} {incr i} {
  74.         set c [string index $text $i]
  75.         if {($c != "\ ") && ($c != "\t")} then {
  76.             return [string range $text 0 [expr $i-1]]
  77.         }
  78.     }
  79.     return
  80. }
  81.  
  82.  
  83. # Brace on new line, same indentation. Insert on another new line, indented in.
  84. # First, see if we are on new line.
  85. proc electricCLeft {} {
  86.     global mode
  87.     global ${mode}modeVars
  88.     deleteText [getPos] [selEnd]
  89.     if {![set ${mode}modeVars(elecLBrace)]} then {
  90.         insertText "\{"
  91.         return
  92.     }
  93.     if {[set ${mode}modeVars(elecLBrace)] && ![catch {search -l [lineStart [expr [lineStart [getPos]] - 1]] -s -f 0 -r 0 "\}" [getPos]} res]} {
  94.         if {[regexp {\}[ \t\r]*else} [getText [lindex $res 0] [expr [getPos] + 1]]]} {
  95.             set res2 [search -f 0 -r 0 {else} [getPos]]
  96.             oneSpace
  97.             set text [getText [lindex $res2 0] [getPos]]
  98.             if {[lookAt [expr [getPos] - 1]] != " "} {
  99.                 append text " "
  100.             }
  101.             replaceText [expr [lindex $res 0] + 1] [getPos] " $text\{\r"
  102.             indentLine
  103.             return
  104.         }
  105.     }
  106.     set pos [getPos]
  107.     set start [lineStart $pos]
  108.     set text [getText $start $pos]
  109.     
  110.     for {set i $start} {$i < $pos} {incr i} {
  111.         set c [lookAt $i]
  112.         if {($c != "\ ") && ($c != "\t")} then {
  113.             break;
  114.         }
  115.     }
  116.     set indentation [getText $start $i]
  117.     if {($i == $pos) || ([lookAt $pos] == " ")} {
  118.         insertText "\{\r" $indentation "\t"
  119.     } else {
  120.         insertText " \{\r" $indentation "\t"
  121.     }
  122. }
  123. bind '\{' <s> electricCLeft C
  124. bind '\{' <s> electricCLeft C++
  125.  
  126.  
  127. # Brace on new line, immediate carriage return
  128. proc electricCRight {} {
  129.     global mode
  130.     global ${mode}modeVars
  131.     
  132.     deleteText [getPos] [selEnd]
  133.     if {[set ${mode}modeVars(elecRBrace)] == "0"} then {
  134.         insertText "\}"
  135.         catch {blink [matchIt "\}" [expr [getPos]-2]]}
  136.         return
  137.     }
  138.     set pos [getPos]
  139.     set start [lineStart $pos]
  140.     
  141.     if {[catch {matchIt "\}" [expr $pos-1]} matched]} {
  142.         beep
  143.         return
  144.     }
  145.     set text [getText [lineStart $matched] $matched]
  146.     regexp {^[     ]*} $text indentation
  147.     for {set i $start} {$i < $pos} {incr i} {
  148.         set c [lookAt $i]
  149.         if {($c != "\ ") && ($c != "\t")} then {
  150.             insertText "\r" $indentation "\}\r" $indentation
  151.             blink $matched
  152.             return
  153.         }
  154.     }
  155.     set text [set indentation]\}\r$indentation
  156.     replaceText $start $pos $text
  157.     goto [expr {$start + [string length $text]}]
  158.     blink [matchIt "\}" [expr $start-2]]
  159. }
  160. bind '\}' <s> electricCRight C
  161. bind '\}' <s> electricCRight C++
  162.  
  163.  
  164. # Brace on new line, immediate carriage return. We don't do our electric stuff
  165. # if we are in the middle of a for statement.
  166. proc electricCSemi {} {
  167.     global mode
  168.     global ${mode}modeVars
  169.     deleteText [getPos] [selEnd]
  170.     if {[set ${mode}modeVars(electricSemi)] == "0"} then {
  171.         insertText ";"
  172.         return
  173.     }
  174.     set pos [getPos]
  175.     set start [lineStart $pos]
  176.     set text [getText $start $pos]
  177.     
  178.     if {[string first "for" $text] != "-1"} {
  179.         set lefts 0
  180.         set rights 0
  181.         set len [string length $text]
  182.         for {set i 0} {$i < $len} {incr i} {
  183.             case [string index $text $i] in {
  184.                 "("    { incr lefts }
  185.                 ")"    { incr rights }
  186.             }
  187.         }
  188.         global globs
  189.         set globs [list $lefts $rights $len]
  190.         if {$lefts != $rights} {
  191.             insertText ";"
  192.             return
  193.         }
  194.     }
  195.     
  196.     insertText ";\r" [indentString $pos]
  197. }
  198. bind '\;' electricCSemi C
  199. bind '\;' electricCSemi C++
  200.  
  201.  
  202. proc ordSemi {} {
  203.     insertText {;}
  204. }
  205.  
  206. bind '\;' <z> ordSemi
  207.  
  208.  
  209. proc cppCR {} {
  210.     if {[lookAt [expr [getPos] - 1]] == ":"} {
  211.         if { [lookAt [getPos]] == "\r" } {
  212.             indentLine
  213.             endOfLine
  214.             carriageReturn
  215.         } else {
  216.             set pos [getPos]
  217.             endOfLine
  218.             set t [getText $pos [getPos]]
  219.             replaceText $pos [getPos] ""
  220.             indentLine
  221.             endOfLine
  222.             carriageReturn
  223.             insertText $t
  224.         }
  225.         indentLine
  226.     } else {
  227.         carriageReturn
  228.         indentLine
  229.     }
  230.     
  231. }
  232.  
  233. bind '\r'     cppCR C
  234. bind '\r'     cppCR C++
  235.         
  236. #================================================================================
  237.  
  238. # proc CMarkFile {} {
  239. #     global CmodeVars
  240. #     set pos 0
  241. #     while {![catch {search -s -f 1 -r 1 -m 0 -i 0 $CmodeVars(funcExpr) $pos} res]} {
  242. #         set start [lindex $res 0]
  243. #         set end [expr [lindex $res 1] + 1]
  244. #         set text [getText $start $end]
  245. #         if {[regexp {([a-zA-Z0-9:_]+)[ \t]*\(} $text dummy word]} {
  246. #             set tmp [expr $start + [string first $word $text]]
  247. #             set inds($word) "$tmp [expr $tmp + [string length $word]]"
  248. #         }
  249. #         set pos $end
  250. #     }
  251. #     if {[info exists inds]} {
  252. #         foreach f [lsort -ignore [array names inds]] {
  253. #             set res $inds($f)
  254. #             setNamedMark $f [lineStart [lindex $res 0]] [lindex $res 0] [lindex $res 1]
  255. #         }
  256. #     }
  257. # }
  258. #     
  259. # #The previous version would not find things like     void    *ThisFunc( xxx ) due to the asterisk
  260. # #I also truncated the pattern.  The rest is not necessary and intrusive as far as I can tell   
  261. # proc C++MarkFile {} {
  262. #     set pos 0
  263. #     while {![catch {search -s -f 1 -r 1 -m 0 -i 0 {^([^ \t\(#\r/@].*[ \t]+)?\*?([A-Za-z0-9:~_]+)[ \t\r]*\(} $pos} res]} {
  264. #         set start [lindex $res 0]
  265. #         set end [expr [lindex $res 1] + 1]
  266. #         set thistext [getText $start $end]
  267. #         #regexp doesn't like carriage returns
  268. #         regsub -all "\r" $thistext " " thistext
  269. #         #regexp doesn't like tabs either
  270. #         regsub -all "\t" $thistext " " thistext
  271. #         #if the open paren was the last character on the line the selected text included the last carriage return as well
  272. #         #trim this off now that it is changed into a space
  273. #         set thistext [string trimright $thistext]
  274. #         if {[regexp {([a-zA-Z0-9:~_]+)[ \t]*\(} $thistext dummy word]} {
  275. #             set inds($word) [lineStart [expr $start - 1]]
  276. #         }
  277. #         set pos $end
  278. #     }
  279. #     if {[info exists inds]} {
  280. #         foreach f [lsort -ignore [array names inds]] {
  281. #             set next [nextLineStart $inds($f)]
  282. #             setNamedMark $f $inds($f) $next $next
  283. #         }
  284. #     }
  285. # }
  286.  
  287.  
  288. proc CMarkFile {} {
  289.     global CmodeVars
  290.     set pos 0
  291.     while {![catch {search -s -f 1 -r 1 -m 0 -i 0 $CmodeVars(funcExpr) $pos} res]} {
  292.         set start [lindex $res 0]
  293.         set end [expr [lindex $res 1] + 1]
  294.         set text [getText $start $end]
  295.         if {[regexp {([a-zA-Z0-9:_]+)[ \t]*\(} $text dummy word]} {
  296.             set tmp [expr $start + [string first $word $text]]
  297.             set inds($word) "$tmp [expr $tmp + [string length $word]]"
  298.         }
  299.         set pos $end
  300.     }
  301.  
  302.     ## 
  303.      # Also    mark any class or struct definitions
  304.      ##
  305.     
  306.     set markExpr {^(class|struct) [A-Za-z0-9_]+[ \t]*(:|\{)}
  307.     while {![catch {search -s -f 1 -r 1 -m 0 -i 0 "$markExpr" $pos} res]} {
  308.         set start [lindex $res 0]
  309.         set end [expr [lindex $res 1] -1]
  310.         set text [string trimright [getText $start $end] ]
  311.         set inds($text) "$start [expr $start + [string length $text]]"
  312.         set pos $end
  313.     }
  314.     if {[info exists inds]} {
  315.         foreach f [lsort -ignore [array names inds]] {
  316.             set res $inds($f)
  317.             setNamedMark $f [lineStart [lindex $res 0]] [lindex $res 0] [lindex $res 1]
  318.         }
  319.     }
  320. }
  321.  
  322. proc C++MarkFile {} {
  323.     set pos 0
  324.     set markExpr {^([^ \t\(#\r/@].*[ \t]+)?\*?([A-Za-z0-9<>~_]+::[-A-Za-z0-9~_+=\*/]+|[A-Za-z0-9~_]+)[ \t\r]*\(}
  325.     while {![catch {search -s -f 1 -r 1 -m 0 -i 0 "$markExpr" $pos} res]} {
  326.         set start [lindex $res 0]
  327.         set end [expr [lindex $res 1] + 1]
  328.         set thistext [getText $start $end]
  329.         #regexp doesn't like carriage returns
  330.         regsub -all "\r" $thistext " " thistext
  331.         #regexp doesn't like tabs either
  332.         regsub -all "\t" $thistext " " thistext
  333.         #if the open paren was the last character on the line the selected text 
  334.         #included the last carriage return as well
  335.         #trim this off now that it is changed into a space
  336.         set thistext [string trimright $thistext]
  337.         if {[regexp {([A-Za-z0-9<>~_]+::[-A-Za-z0-9~_+=\*/]+|[A-Za-z0-9~_]+)[ \t]*\(} $thistext dummy word]} {
  338.             if { [string first "::" $word] != -1 } {
  339.                 regsub {(<[A-Za-z0-9_]+>)?::} $word " " it
  340.                 set l [lindex $it 0]
  341.                 if { $l == [lindex $it 1] } {
  342.                     set word "Construct '$l'"
  343.                 } elseif { "~$l" == [lindex $it 1] } {
  344.                     set word "Destruct '$l'"
  345.                 }
  346.             }
  347.             set inds($word) [lineStart [expr $start - 1]]
  348.         }
  349.         set pos $end
  350.     }
  351.     if {[info exists inds]} {
  352.         foreach f [lsort -ignore [array names inds]] {
  353.             set next [nextLineStart $inds($f)]
  354.             # Alpha doesn't like '<' or '>' in the mark menu
  355.             regsub -all {[<>]+} $f "|" it
  356.             if {[string length $it] > 35} { set it "[string range $it 0 31]..." }
  357.             setNamedMark "${it}" "$inds($f)" $next $next
  358.         }
  359.     }
  360. }
  361.  
  362. proc setC++Mode {} {
  363.     changeMode "C++"
  364. }
  365.  
  366.  
  367.  
  368. source "$HOME:Tcl:SystemCode:think.tcl"
  369.  
  370. proc dummyC {} {}
  371. proc dummyC++ {} {}
  372.  
  373.  
  374. #===============================================================================
  375.  
  376. proc CDblClick {from to} {
  377.     global tagFile
  378.     
  379.     select $from $to
  380.     set text [getSelect]
  381.     
  382.     set lines [grep "^$text'" $tagFile]
  383.     if {[regexp {'(.*)'(.*[^\t])(\t)+░} $lines dummy one two]} {
  384.         if {[string match "*$one*" [winNames -f]]} {
  385.             bringToFront $one
  386.         } else {
  387.             edit $one
  388.         }
  389.         set inds [search -f 1 -r 0 "$two" 0]
  390.         display [lindex $inds 0]
  391.         eval select $inds
  392.     } else {
  393.         checkRunning ThinkReference DanR referencePath
  394.         AEBuild {'DanR'} DanR {REF } "----" "╥$text╙"
  395.     }
  396. }
  397.  
  398. proc C++DblClick {from to shift option control} {
  399.     CDblClick $from $to
  400. }
  401.